﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AmkeniCrossLoginApplication.Data_Access;
using Amkeni_System.Helpers;

namespace Amkeni_System.Controls
{
    public partial class IP_ImageGrid : System.Web.UI.UserControl
    {
        private string virtualPath;
        private string physicalPath;

        /// <summary>
        /// Relative path to the Images Folder
        /// </summary>
        public string ImageFolderPath { get; set; }

        /// <summary>
        /// Caption to be displayed on top of Images
        /// </summary>
        public string Caption { get; set; }

        public string IP_ID { get; set; }

        public string Report_Period_ID { get; set; }

        public string Year_ID { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Update the Path
            UpdatePath();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            //Bind the data before Rendering
            BindData();
        }

        private void UpdatePath()
        {
            //Use default path
            virtualPath = "~/Amkeni_System/ip_photos";
            physicalPath = Server.MapPath(virtualPath);

            //If ImageFolderPath is specified then use that path
            if (!string.IsNullOrEmpty(ImageFolderPath))
            {
                physicalPath = Server.MapPath(ImageFolderPath);
                virtualPath = ImageFolderPath;
            }
        }

        private void BindData()
        {
            var images = new List<string>();

            try
            {
                Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();
                int ip_id = int.Parse(IP_ID.Trim());
                int period_id = int.Parse(Report_Period_ID.Trim());
                int year_id = int.Parse(Year_ID.Trim());

                var imageData = from data in db.ip_photo
                                where data.ip_id == ip_id && data.report_period_id == period_id
                                    && data.year_id == year_id
                                select new { data.name };

                foreach (var item in imageData)
                {
                    images.Add(string.Format("{0}/{1}", virtualPath, item.name));
                }
            }
            catch (Exception ex)
            {
                //log Exception
                //Logger.Log(ex.Message);
            }
            ImageListView2.DataSource = images;
            ImageListView2.DataBind();

            ImageListView.DataSource = images;
            ImageListView.DataBind();
        }

        protected void ImageListView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }
    }
}